package com.frcdi.qms.masterdata.service.impl;

import java.util.List;

import com.frcdi.qms.common.constant.UserConstants;
import com.frcdi.qms.common.utils.DateUtils;
import com.frcdi.qms.common.utils.SecurityUtils;
import com.frcdi.qms.common.utils.StringUtils;
import com.frcdi.qms.masterdata.domain.ChangeExecution;
import com.frcdi.qms.masterdata.domain.CodeConfig;
import com.frcdi.qms.masterdata.mapper.ChangeExecutionMapper;
import com.frcdi.qms.masterdata.mapper.CodeConfigMapper;
import com.frcdi.qms.masterdata.domain.Material;
import com.frcdi.qms.masterdata.mapper.MaterialMapper;
import com.frcdi.qms.masterdata.service.IMaterialService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 物料Service业务层处理
 *
 * @author cell
 * @date 2023-09-11
 */
@Service
public class MaterialServiceImpl implements IMaterialService
{

    private static final Logger log = LoggerFactory.getLogger(MaterialServiceImpl .class);
    @Autowired
    private MaterialMapper materialMapper;

    @Autowired
    private ChangeExecutionMapper executionMapper;

    @Autowired
    private CodeConfigMapper codeMapper;

    /**
     * 查询物料
     *
     * @param id 物料主键
     * @return 物料
     */
    @Override
    public Material selectMaterialById(Long id)
    {
        return materialMapper.selectMaterialById(id);
    }

    /**
     * 查询物料列表
     *
     * @param material 物料
     * @return 物料
     */
    @Override
    public List<Material> selectMaterialList(Material material)
    {
        return materialMapper.selectMaterialList(material);
    }

    /**
     * 新增物料
     *
     * @param material 物料
     * @return 结果
     */
    @Override
    public String insertMaterial(Material material)
    {
        materialMapper.insertMaterial(material);
        return material.getId().toString();
    }

    /**
     * 修改物料
     *
     * @param material 物料
     * @return 结果
     */
    @Override
    public int updateMaterial(Material material)
    {
        int row = materialMapper.updateMaterial(material);
        return row;
    }
    @Override
    public int updateMaterialByMaterialNo(Material material)
    {
        int row = materialMapper.updateMaterialByMaterialNo(material);
        return row;
    }

    /**
     * 批量删除物料
     *
     * @param ids 需要删除的物料主键
     * @return 结果
     */
    @Override
    public int deleteMaterialByIds(Long[] ids)
    {
        int row = materialMapper.deleteMaterialByIds(ids);
        return row;
    }

    /**
     * 删除物料信息
     *
     * @param id 物料主键
     * @return 结果
     */
    @Override
    public int deleteMaterialById(Long id)
    {
        materialMapper.deleteMaterialById(id);
        return materialMapper.deleteMaterialById(id);
    }

    @Override
    public boolean checkmaterialNameUnique(Material material) {
        Long materialId = StringUtils.isNull(material.getId()) ? -1L : material.getId();
        Material info = materialMapper.checkMaterialNameUnique(material.getMaterialName(), material.getMaterialType());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != materialId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Transactional
    @Override
    public int submitApplicant(Material material) {
        // 修改物料返回状态
        Material upMaterial = new Material();
        upMaterial.setId(material.getId());
        upMaterial.setSend(UserConstants.SEND_HAS_RETURNED);
        materialMapper.updateMaterial(upMaterial);
        // 新增变更执行单
        ChangeExecution execution = new ChangeExecution();
        execution.setChangeNo(material.getChangeNo());
        execution.setAssociatedId(material.getId());
        execution.setAssociatedName(material.getMaterialName());
        execution.setAssociatedType(UserConstants.MATERIAL);
        execution.setCreateBy(SecurityUtils.getUsername());
        return executionMapper.insertChangeExecution(execution);
    }

    @Override
    public String getCode() {
        String syscode = new StringBuilder().toString();
        // 获取前缀
        CodeConfig codeConfig = codeMapper.selectCodeConfigByCodeId(4L);
        if (StringUtils.equals(UserConstants.CODE_ADDTIME,codeConfig.getCodeTime())){
            syscode = codeConfig.getCodeRule()+ DateUtils.dateTime();
        }else {
            syscode = codeConfig.getCodeRule();
        }
        // 获取最近一次生成
        String code = materialMapper.getCode(syscode);

        if (StringUtils.isNull(code)){
            return syscode + StringUtils.padl(1,codeConfig.getCodeNumber());
        }

        String newcode = StringUtils.substring(code, code.length() - codeConfig.getCodeNumber(), code.length());
        return syscode + (StringUtils.padl(Integer.valueOf(newcode)+1,codeConfig.getCodeNumber()));
    }
}
